<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
	<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css">
	<link rel="stylesheet" href="css/login.css">
</head>

<body>
	<div class="container">
        <div class="row align-items-center">
            <div class="col">

            </div>
            <div class="col">
                <div class="alert alert-danger alert-dismissible fade show invisible text-center" style="width: 42rem;"
                    role="alert">
                    <strong>提示：</strong><label for="" id="msg">不得行</label>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            </div>
            <div class="col">

            </div>

        </div>

        <div class="align-items-center yx">
            <div class="col">
                
            </div>
            <div class="col">
                <form id="loginForm">
						<div class="main_box">
							<div class="login_box">
								<h3><span>用户登录</span><a href="/register" class="mr">立即注册</a></h3>
								<div class="form_all_box">
									<input type="text" id="username" class="form_box" name="username" placeholder=用户名>			
								</div>
								<div class="form_all_box">
									<input type="password" id="password" class="form_box" name="password" placeholder="密码">
								</div>
								<div class="select_btn">
									
									<h4 class="mr">
										<span class="forget_pw"><a href="/forgot">忘记密码？</a></span>
									</h4>
								</div>
								<div class="form_all_box">
									<button type="button" class="btn_box" onclick="jump()">登录</button>
								</div>
							</div>
                    </div>
                </form>
            <div class="col">
                
            </div>
        </div>

    </div>



	<script src="js/jquery-3.6.0.min.js"></script>
    <script src="css/bootstrap/bootstrap.min.js"></script>
	
	<script>
		 function jump() {
            let username = $('#username').val();
            let password = $('#password').val();
            if (username.length > 0 && password.length > 0) {
                // ajax请求
                $.ajax({
                    url: "/loginDone",
                    type: "POST",
                    data: $('#loginForm').serialize(),
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if(res.code === 200){
                            window.location.href="/";
                        }else{
                            alert_fn(res.msg);
                        }                       
                    },
                    error: function (msg) {
                        alert_fn('网络或者服务器有问题');
                    }
                })
            } else {// 验证不通过，提示相关信息，并且阻止提交
                alert_fn('账号和密码不能为空');
                return false;
            }
        }

        // 这个方法包装了alert的相关行为和显示的内容
        function alert_fn(msg) {
            msg = msg || '密码不能为空，两次密码应该一致';
            // 替换提示的内容
            $('#msg').text(msg);
            // 移除不显示的class，则alert将显示
            $('.invisible').removeClass('invisible');
            // 三秒中后，给alert增加不显示的class，则alert重新隐藏
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
	</script>
</body>
</html>